home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / answrbok / 3_11.lha / 3_11 / 3_11d.c < prev    next >
C/C++ Source or Header  |  1993-08-08  |  679b  |  39 lines

  1. * Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
  2. * The C++ Answer Book */
  3. * Tony Hansen */
  4. * All rights reserved. */
  5. / convert an integer into a string of max length len
  6. / version 1
  7. include <swap.h>
  8. include <error.h>
  9.  
  10. oid itoa(char *s, int len, int val)
  11.  
  12.    // reverse the sign of negative numbers
  13.    if (val < 0)
  14. {
  15. if (--len <= 0)
  16.     error("string not long enough");
  17.  
  18. *s++ = '-';
  19. val = -val;
  20. }
  21.  
  22.    // store digits in reverse order
  23.    char *p = s;
  24.  
  25.    do  {
  26. if (--len <= 0)
  27.     error("string not long enough");
  28.  
  29. *p++ = val % 10 + '0';
  30. val /= 10;
  31.    } while (val != 0);
  32.  
  33.    *p-- = '\0';
  34.  
  35.    // swap the digits around
  36.    while (s < p)
  37. swap(*s++, *p--);
  38.  
  39.